package com.example.restatr.Atcivity.Bus;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Bus.Bean.BusOrderBean;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class BusOrderAt extends AppCompatActivity {
    private List<BusOrderBean.RowsBean> busRowsBeans = new ArrayList<>();
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private ListView list;
    private LinearLayout Null;
    private ImageView nullIcon;
    private TextView nullTitle;
    private adapter adapter=new adapter();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus_order_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        list = findViewById(R.id.list);
        Null = findViewById(R.id.Null);
        nullIcon = findViewById(R.id.null_icon);
        nullTitle = findViewById(R.id.null_title);
        Tools.SetBarTitle(toolbar, barTitle, "我的订单", search, this,false);
        GetBusOrderList();
    }

    //获取巴士订单
    private void GetBusOrderList() {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/bus/order/list", MainActivity.sp.getString("token", ""));
            BusOrderBean busOrderBean = new Gson().fromJson(token, BusOrderBean.class);
            List<BusOrderBean.RowsBean> collect = busOrderBean.getRows().stream().sorted(Comparator.comparing(BusOrderBean.RowsBean::getCreateTime).reversed()).collect(Collectors.toList());
            busRowsBeans.addAll(collect);
            runOnUiThread(() -> {
                if (busRowsBeans.size() > 0) {
                    list.setAdapter(adapter);
                } else {
                    Null.setVisibility(View.VISIBLE);
                }
            });

        }).start();
    }

    class adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return busRowsBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return busRowsBeans.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.bus_order, null);
            TextView path = convertView.findViewById(R.id.path);
            TextView diz = convertView.findViewById(R.id.diz);
            TextView orderNum = convertView.findViewById(R.id.orderNum);
            TextView createTime = convertView.findViewById(R.id.createTime);
            TextView status = convertView.findViewById(R.id.status);
            TextView price = convertView.findViewById(R.id.price);
            Button pay = convertView.findViewById(R.id.pay);
            path.setText(busRowsBeans.get(position).getPath());
            diz.setText("起始站：" + busRowsBeans.get(position).getStart() + " 终点站：" + busRowsBeans.get(position).getEnd());
            orderNum.setText("订单号：" + busRowsBeans.get(position).getOrderNum());
            createTime.setText("创建时间：" + busRowsBeans.get(position).getCreateTime());
            int statusInt = busRowsBeans.get(position).getStatus();
            if (statusInt == 0) {
                status.setText("待支付");
                status.setTextColor(Color.parseColor("#FD815A"));
                pay.setVisibility(View.VISIBLE);
            } else {
                status.setText("已支付");
                status.setTextColor(Color.parseColor("#09F175"));
            }
            price.setText("票价：" + busRowsBeans.get(position).getPrice() + "¥");
            pay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    payOrder(busRowsBeans.get(position).getOrderNum(),position);
                }
            });

            return convertView;
        }
    }

    private void payOrder(String orderNum,int pos) {
        new Thread(() -> {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("orderNum", orderNum);
                jsonObject.put("paymentType", "电子支付");
                String post = Tools.Post("/prod-api/api/bus/pay", MainActivity.sp.getString("token", ""), jsonObject.toString());
                JSONObject jx = new JSONObject(post);
                String msg = jx.getString("msg");
                int code = jx.getInt("code");
                runOnUiThread(() -> {
                    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
                    if (code==200){
                    busRowsBeans.get(pos).setStatus(1);
                    adapter.notifyDataSetChanged();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }).start();
    }
}